package jellis.android.image;

import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


public class Image extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Button getImageButton = (Button)findViewById(R.id.getImageButton);
		String color="&chco=0000FF,FF0000";
		String legenda="&chdl=Gemiddeld|Uw kind";
		String data="&chd=t:4.2,5.0,5.9|4.18,5.03,5.97";
		final String url2 =   "http://chart.apis.google.com/chart?cht=lc&chs=250x200&chxt=x,y&chxr=0,0,12|1,0,10&chds=4,6"+data+color+legenda;
		final String url = "http://chart.apis.google.com/chart?cht=lc&chs=250x200&chxt=x,y&chxr=0,0,12|1,3,10&chd=t:4.2,5.0,5.9,6.5,7.1,7.7,8.1,8.4,8.8,9.05,9.45,9.75|4.18,5.03,5.97&chco=0000FF,FF0000&chdl=Gemiddeld|Uw%20kind&chds=3,10";
		
		getImageButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Context context = view.getContext();
				Drawable image = ImageOperations(context,url,"image.png");
				ImageView imgView = new ImageView(context);
				imgView = (ImageView)findViewById(R.id.image1);
				imgView.setImageDrawable(image);
		}});

	}	

	private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}

}